<?php

declare(strict_types=1);

namespace app\model;

use app\common\facade\auth\Auth;
use app\common\service\auth\AuthAdminService;
use app\common\service\auth\AuthAgentService;
use app\model\base\BaseTakeoutSliderModel;
use think\db\Query;
use think\facade\Db;
use think\model\concern\SoftDelete;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $id 自增id
 * @property int $agent_id 代理商id
 * @property string $name 轮播图名称
 * @property string $image 轮播图图片
 * @property int $sort 排序，desc
 * @property int $status 状态：1启用 -1禁用
 * @property string $params 跳转参数
 * @property int $is_deleted 是否删除：1是 0否
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property bool $type 1.外卖2.聚合模板 3.权益
 * @property int $position 轮播图位置 1.头部 2.中间
 * @method static \think\db\Query onlyTrashed()
 * @method static \think\db\Query withTrashed()
 */
class TakeoutSlider extends BaseTakeoutSliderModel
{
    use SoftDelete;
    protected $deleteTime = 'is_deleted';
    protected $defaultSoftDelete = 0;

    //类别type
    const TYPE_TACKOUT = 1;  //外卖系统
    const TYPE_UNION   = 2;  //本地生活
    const TYPE_COUPON  = 3;  //会员卡券

    //状态
    const STATUS_ACTIVE = 1;  //启用
    const STATUS_CLOSE  = -1; //禁用

    //删除状态
    const ISDELETED_YES = 1;  //已删除
    const ISDELETED_NO  = 0;  //未删除

    //轮播图位置
    const POSITION_HEAD = 1;   //头部
    const POSITION_MIDDLE = 2; //中间



    protected $auth;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->auth = Auth::instance();
    }


    /**
     * @note 轮播图列表
     * @access public
     * @param $params array 参数
     * @return array
     */
    public function getList($params)
    {
        //代理端
        if (Auth::platform() == 'agent'){
            $params['agent_id'] = Auth::agentId();
        }

        //总后台
        if (Auth::platform() == 'admin'){
            $params['agent_id'] = 0;
        }

        return self::query()->withSearch(['agent_id', 'type', 'status', 'position'], $params)
            ->order('sort', 'desc')
            ->paginate();
    }

    /**
     * 代理搜索
     */
    public function searchAgentIdAttr($query, $value, $data)
    {
        /**
         * @var Query $query
         */
        //代理需要看到自己的并且看到总后台设置的
        if (Auth::platform() == 'agent'){
            $query->whereRaw('agent_id = '.$value.' OR agent_id = 0');
        } else {
            $query->where('agent_id', 0);
        }
    }

    /**
     * 类型搜索
     */
    public function searchTypeAttr($query, $value, $data)
    {
        if ($value && in_array($value, [self::TYPE_TACKOUT, self::TYPE_UNION, self::TYPE_COUPON])) {
            $query->where('type', $value);
        }
    }

    /**
     * 状态搜索
     */
    public function searchStatusAttr($query, $value, $data)
    {
        if ($value && in_array($value, [self::STATUS_ACTIVE, self::STATUS_CLOSE])) {
            $query->where('status', $value);
        }
    }

    /**
     * 轮播图位置搜索
     */
    public function searchPositionAttr($query, $value, $data)
    {
        if ($value && in_array($value, [self::POSITION_HEAD, self::POSITION_MIDDLE])) {
            $query->where('position', $value);
        }
    }

    /**
     * 新增
     */
    public function add($params)
    {
        $slider = self::create([
            'agent_id' => $params['agent_id']??0,
            'name'     => $params['name'],
            'image'    => $params['image'],
            'params'   => json_encode([
                'type' => $params['jump_type'],
                'path' => $params['path'],
            ]),
            'sort' => $params['sort'],
            'type'     => $params['type'],
        ]);
        return $slider->id;
    }

}
